//
//  ForumViewController.m
//  B段项目
//
//  Created by NeyMAr on 15/9/23.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "ForumViewController.h"
#import "ForumModel.h"
#import "ForumCell.h"
#import "ForumNextViewController.h"
#import <AFNetworking.h>
#import <MJRefresh.h>
#import "AFNetWorkingW.h"
@interface ForumViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation ForumViewController

- (void)viewWillAppear:(BOOL)animated
{
    SelfTabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.title = @"麦奇论坛";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.067 green:0.694 blue:0.651 alpha:1.000];
    self.navigationController.navigationBar.titleTextAttributes = @{@"Font":[UIFont fontWithName:nil size:22], @"TextColor":[UIColor blackColor]};
    
    
    [self tableViewSubview];
    [self dataHandel];

}


- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * w, 667 * h) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 90;

    
/**下拉刷新     ***********/
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
/**上拉加载     ***********/
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}


- (void)loadNewData
{
    [self.tableView.header beginRefreshing];
    
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:@"http://my.xcar.com.cn/app/6/getHotPostList.php?offset=0&limit=20&ver=6.1.6" resultBlock:^(id result) {
        NSMutableArray *arr =[NSMutableArray array];
        arr = [result objectForKey:@"postList"];
        self.arr = [NSMutableArray array];
        
        for (NSMutableDictionary *dic in arr) {
            
            ForumModel *model = [[ForumModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            
        }
        [self.tableView reloadData];
    }];
    
    [self.tableView.header endRefreshing];
}


NSInteger i = 10;
- (void)loadMoreData
{
    [self.tableView.footer beginRefreshing];
    NSString *str = [NSString stringWithFormat:@"http://my.xcar.com.cn/app/6/getHotPostList.php?offset=%ld&limit=20&ver=6.1.6", i];
    i += 10;
    
    [AFNetWorkingW netWorkingHanderGetGataWithUrl:str resultBlock:^(id result) {
        NSMutableArray *arr =[NSMutableArray array];
        arr = [result objectForKey:@"postList"];
        
        for (NSMutableDictionary *dic in arr) {
            
            ForumModel *model = [[ForumModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            
        }
        [self.tableView reloadData];
    }];
    
    [self.tableView.footer endRefreshing];
}


#pragma mark - 数据解析
- (void)dataHandel
{
    /* 菊花出现 */
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32 * w , 32 * h)];
    self.activityIndicator.backgroundColor = [UIColor blackColor];
    _activityIndicator.center = CGPointMake(w * 375 / 2.0, h * 667 / 2.0 - h * 50);
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.view addSubview:_activityIndicator];
    [_activityIndicator startAnimating]; /* 开始出现菊花 */

    [AFNetWorkingW netWorkingHanderGetGataWithUrl:@"http://my.xcar.com.cn/app/6/getHotPostList.php?offset=0&limit=20&ver=6.1.6" resultBlock:^(id result) {
        NSMutableArray *arr =[NSMutableArray array];
        arr = [result objectForKey:@"postList"];
        self.arr = [NSMutableArray array];

        for (NSMutableDictionary *dic in arr) {

            ForumModel *model = [[ForumModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];

        }
        [self.tableView reloadData];
        /*菊花消失*/
        [_activityIndicator stopAnimating];
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arr count];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[ForumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    
    ForumModel *model = [[ForumModel alloc] init];
    model = [self.arr objectAtIndex:indexPath.row];
    cell.model = model;

    return cell;
}


#pragma mark - 点击进入web页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumNextViewController *next = [[ForumNextViewController alloc] init];
    ForumModel *model = [[ForumModel alloc] init];
    model = [self.arr objectAtIndex:indexPath.row];
    next.strUrl = model.postLink;
    
    [self.navigationController pushViewController:next animated:YES];
    
}







#pragma mark - Memory
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
